/** 
* Copyright (C) 2012-2018 ZEEI Inc.All Rights Reserved.
* 项目名称：pls
* 文件名称：PointSynthesizeGisServiceImpl.java
* 包  名  称：com.zeei.gis.service.imp
* 文件描述：TODO 请修改文件描述
* 创建日期：2018年1月9日上午10:08:03
* 
* 修改历史
* 1.0 jiangqingpeng 2018年1月9日上午10:08:03 创建文件
*
*/

package com.zeei.gis.service.imp;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zeei.common.converter.DateUtil;
import com.zeei.gis.dao.AreaCodeGisDao;
import com.zeei.gis.dao.DPointListGisVODao;
import com.zeei.gis.dao.IDPointListGisVODao;
import com.zeei.gis.dao.PointListGisVODao;
import com.zeei.gis.dao.TradeCodeGisDao;
import com.zeei.gis.service.PointSynthesizeGisService;
import com.zeei.gis.utils.AreaCodeGisHandle;
import com.zeei.gis.vo.AreaCodeGisVO;
import com.zeei.gis.vo.ConcentrationTimeGisVO;
import com.zeei.gis.vo.DPointListGisVO;
import com.zeei.gis.vo.OutletQueryCondition;
import com.zeei.gis.vo.PointListGisVO;
import com.zeei.gis.vo.PointQuantityByGroupGisVO;
import com.zeei.gis.vo.PointSynthesizeGisVO;
import com.zeei.gis.vo.TradeCodeGisVO;
import com.zeei.gis.vo.TwentyFourHourMonitorDataGisVO;
import com.zeei.gis.vo.TwentyFourHourMonitorQueryConditionGisVO;
import com.zeei.gis.vo.ListPointGisVO;

/**
 * @类型名称：PointSynthesizeGisServiceImpl
 * @类描述：TODO 废气废水排口综合Service接口实现类
 * @功能描述：TODO 废气废水排口综合Service接口实现类
 * @创建作者：jiangqingpeng
 */
@Service("pointSynthesizeGisService")
public class PointSynthesizeGisServiceImpl implements PointSynthesizeGisService{
	
	@Autowired
	PointListGisVODao pointListGisVODao;

	@Autowired
	DPointListGisVODao dPointListGisVODao;
	
	@Autowired
	IDPointListGisVODao idPointListGisVODao;
	
	@Autowired
	AreaCodeGisDao areaCodeGisDao;
	
	@Autowired
	TradeCodeGisDao tradeCodeGisDao;
	
	/**
	 * queryPointSynthesizeByAreacode:TODO 通过多种条件查询排口信息
	 *
	 * @param areacode
	 * @return List<PointSynthesizeGisVO>
	 */
	@Override
	public PointSynthesizeGisVO queryPointSynthesizeByCondition(OutletQueryCondition outletQueryCondition) {
		
		//经过处理的地区代码
		String disposeAreacode = AreaCodeGisHandle.getDisposeAreacode(outletQueryCondition.getAreacode());
		outletQueryCondition.setDisposeAreacode(disposeAreacode);
		List<ListPointGisVO> listPoint = null;	//废气排口详情
		Long directWaterNum = null;	//废气排口数量
		Long airPointNum = null;	//废水直接排口数量
		Long indirectWaterNum = null;	//废水间接排口数量
		Map<String, Long> pointquantity = new HashMap<>();	//存放排口数量
		String condition = outletQueryCondition.getPollutionDischargeType();
		if (condition== null || condition.equals("")) {
			condition = "all";
		}
		switch (condition) {
		case "air":
			listPoint = pointListGisVODao.queryAirPointSynthesize(outletQueryCondition);
			//废气排口数量
			airPointNum = pointListGisVODao.queryPointQuantity(outletQueryCondition);
			break;
		case "water":
			//废水直接排口
			listPoint = dPointListGisVODao.queryWaterPointSynthesize(outletQueryCondition);
			//废水直接排口数量
			directWaterNum = dPointListGisVODao.queryPointQuantity(outletQueryCondition);
			//废水间接排口数量
			indirectWaterNum = idPointListGisVODao.queryPointQuantity(outletQueryCondition);
			break;
		default:
			listPoint = pointListGisVODao.queryPointSynthesizeAll(outletQueryCondition);
			//废气排口数量
			airPointNum = pointListGisVODao.queryPointQuantity(outletQueryCondition);
			//废水直接排口数量
			directWaterNum = dPointListGisVODao.queryPointQuantity(outletQueryCondition);
			//废水间接排口数量
			indirectWaterNum = idPointListGisVODao.queryPointQuantity(outletQueryCondition);
			break;
		}

		PointSynthesizeGisVO pointSynthesizeGisVO = new PointSynthesizeGisVO();
		pointquantity.put("airpointquantity", airPointNum);
		if (directWaterNum == null && indirectWaterNum == null) {
			pointquantity.put("waterpointquantity", null);
		} else {
			pointquantity.put("waterpointquantity", (directWaterNum + indirectWaterNum));
		}
		if (listPoint != null) {
			pointSynthesizeGisVO.setListPoint(listPoint);
		}
		pointSynthesizeGisVO.setPointquantity(pointquantity);
		return pointSynthesizeGisVO;
	}

	/**
	 * queryOutletByPointCode:TODO 通过废气排口编码查询废气排口详情
	 *
	 * @param pointcode
	 * @return List<PointListGisVO>
	 */
	@Override
	public List<PointListGisVO> queryOutletByPointCode(String pointcode) {
		List<PointListGisVO> list = pointListGisVODao.queryOutletByPointCode(pointcode);
		return list;
		
	}

	/**
	 * queryPointByPointcode:TODO 通过废水排口编码查询废气排口详情
	 *
	 * @param pointcode
	 * @return List<PointListGisVO>
	 */
	@Override
	public List<DPointListGisVO> queryPointByPointcode(String pointcode) {
		List<DPointListGisVO> list = dPointListGisVODao.queryPointByPointcode(pointcode);
		return list;
		
	}

	/**
	 * queryAirTwentyFourHourMonitorData:TODO 通过排口编号和当前时间查询实时污染物浓度
	 *
	 * @param TwentyFourHourMonitorQueryConditionGisVO
	 * @return  List<TwentyFourHourMonitorDataGisVO>
	 */
	@Override
	public List<TwentyFourHourMonitorDataGisVO> queryAirTwentyFourHourMonitorData(
			TwentyFourHourMonitorQueryConditionGisVO tfhqc) {
		if (tfhqc.getCurrenttime() == null) {
			tfhqc.setCurrenttime(dateFormat(new Date()));
		} else {
			SimpleDateFormat format = new SimpleDateFormat(DateUtil.TIMEF_FORMAT);
			try {
				tfhqc.setCurrenttime(dateFormat(format.parse(tfhqc.getCurrenttime())));
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		List<TwentyFourHourMonitorDataGisVO> list = null;
		if (tfhqc.getPointType().equals("air")) {
			 list = pointListGisVODao.queryAirTwentyFourHourMonitorData(tfhqc);
		} else if (tfhqc.getPointType().equals("water")) {
			list = dPointListGisVODao.queryWaterTwentyFourHourMonitorData(tfhqc);
		}
		if (list != null) {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date d = null;
			String curDate = null;
			try {
				 d = sdf.parse(tfhqc.getCurrenttime());
				 Calendar calendar = Calendar.getInstance();  
			        calendar.setTime(d);  
			        calendar.add(Calendar.DAY_OF_MONTH, -1);  //前一天的时间
			        d = calendar.getTime();  
			        curDate = sdf.format(d);
			} catch (ParseException e1) {
				e1.printStackTrace();
			}
			for (int i = 0; i < list.size(); i++) {
				String s1 = addDateMinut(curDate, 1);
				List<ConcentrationTimeGisVO> listConcentration = list.get(i).getListConcentration();
				for(int j2 = 0; j2 < 24; j2++){
					int j = 0;
					for (; j < listConcentration.size(); j++) {
						if (dateFormat(s1).equals(dateFormat(listConcentration.get(j).getCurrentTime())) ) {
							//存在当前污染物数据，退出当前循环进行下一次循环
							s1 = addDateMinut(s1, 1);	//加一个小时
							break;
						}
						
					}	//j 循环结束
					if (j == listConcentration.size() && !listConcentration.get(j-1).getCurrentTime().equals(dateFormat(s1))) {
						//如果不存在当前时间的污染物数据，则添加当前小时并且浓度默认为0
						list.get(i).getListConcentration().add(new ConcentrationTimeGisVO(s1, 0));	
						s1 = addDateMinut(s1, 1);	//加一个小时
						continue;
					}
					
				}	//j2 循环结束
				Collections.sort(list.get(i).getListConcentration());
				//截取日期的小时
				for(int j = 0; j < list.get(i).getListConcentration().size(); j++){
					String s = list.get(i).getListConcentration().get(j).getCurrentTime();
					list.get(i).getListConcentration().get(j).setCurrentTime(s.substring(11, 13));
				}
			}
			return list;
		}
		
		return null;
		
	}

	/**
	 * 
	 * dateFormat:TODO 格式化日期，够着日期格式为：yyyy-MM-dd hh:00:00
	 *
	 * @param s
	 * @return
	 * @throws ParseException String
	 */
	private static String dateFormat(Date date2) {
		SimpleDateFormat format = new SimpleDateFormat(DateUtil.DATE_HOUR_FORMAT);
		String date = format.format(date2) + ":00:00";
		return date;
	}
	
	/**
	 * 
	 * dateFormat:TODO 格式化日期，够着日期格式为：yyyy-MM-dd hh:00:00
	 *
	 * @param s
	 * @return
	 * @throws ParseException String
	 */
	private static String dateFormat(String str) {
		return str.substring(0, 13);
	}
	
	/**
     * 给时间加上几个小时
     * @param day 当前时间 格式：yyyy-MM-dd HH:mm:ss
     * @param hour 需要加的时间
     * @return
     */
    public static String addDateMinut(String day, int hour){   
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;   
        try {   
            date = format.parse(day);   
        } catch (Exception ex) {   
            ex.printStackTrace();   
        }   
        if (date == null)   
        return "";   
        Calendar cal = Calendar.getInstance();   
        cal.setTime(date);   
        cal.add(Calendar.HOUR, hour);// 24小时制   
        date = cal.getTime();   
        cal = null;   
        return format.format(date);   

    }

    /**
	 * queryPointQuantityByAreacodeGroup:TODO 查询每个市排口数据
	 *
	 * @return List<PointQuantityByAreacodeGroupGisVO>
	 */
	@Override
	public List<PointQuantityByGroupGisVO> queryPointQuantityByAreacodeGroup() {
		List<AreaCodeGisVO> listArea = areaCodeGisDao.queryCityAreaCodeGisList();
		Long airPointQuantity = null;	//废气排口数据
		Long dwaterPointQuantity = null;	//废水直接排口数量
		Long idwaterPointQuantity = null;	//废水间接排口数量
		List<PointQuantityByGroupGisVO> list = new ArrayList<>();
		for (AreaCodeGisVO areaCodeGisVO : listArea) {
			String areacode = areaCodeGisVO.getCodeRegion().substring(0, 4);
			if (areacode.substring(2,4).equals("00")) {
				areacode = areacode.substring(0,2);
			}
			String areaname = areaCodeGisVO.getRegionname();
			//废气排口数量
			airPointQuantity = pointListGisVODao.queryPointQuantityByAreacode(areacode);
			//废水排口数量
			dwaterPointQuantity = dPointListGisVODao.queryPointQuantityByAreacode(areacode);
			//废水间接排口数量
			idwaterPointQuantity = idPointListGisVODao.queryPointQuantityByAreacode(areacode);
			Long total = airPointQuantity + dwaterPointQuantity + idwaterPointQuantity;
			list.add(new PointQuantityByGroupGisVO(areaname, total));
		}
		return list;
		
	}

	/**
	 * queryPointQuantityByTradecodeGroup:TODO 查询每个行业排口数量
	 *
	 * @return List<PointQuantityByGroupGisVO>
	 */
	@Override
	public List<PointQuantityByGroupGisVO> queryPointQuantityByTradecodeGroup() {
		List<TradeCodeGisVO> listTrade = tradeCodeGisDao.queryTradecodeGisList();
		Long airPointQuantity = null;	//废气排口数据
		Long dwaterPointQuantity = null;	//废水直接排口数量
		Long idwaterPointQuantity = null;	//废水间接排口数量
		List<PointQuantityByGroupGisVO> list = new ArrayList<>();
		for (TradeCodeGisVO tradeCodeGisVO : listTrade) {
			String tradecode = tradeCodeGisVO.getCodeTrade();
			String tradename = tradeCodeGisVO.getTradeName();
			//废气排口数量
			airPointQuantity = pointListGisVODao.queryPointQuantityByTradecode(tradecode);
			//废水排口数量
			dwaterPointQuantity = dPointListGisVODao.queryPointQuantityByTradecode(tradecode);
			//废水间接排口数量
			idwaterPointQuantity = idPointListGisVODao.queryPointQuantityByTradecode(tradecode);
			Long total = airPointQuantity + dwaterPointQuantity + idwaterPointQuantity;
			list.add(new PointQuantityByGroupGisVO(tradename, total));
		}
		return list;
		
	}

	/**
	 * queryPointQuantityByControllEvel:TODO 查询每个控制级别排口数量
	 *
	 * @return List<PointQuantityByGroupGisVO>
	 */
	@Override
	public List<PointQuantityByGroupGisVO> queryPointQuantityByControllEvel() {
		//国控、省控、市控废气排口数量
		List<PointQuantityByGroupGisVO> listAir = pointListGisVODao.queryPointQuantityByControllEvel();
		//国控、省控、市控废水直接排口数量
		List<PointQuantityByGroupGisVO> listDWater = dPointListGisVODao.queryPointQuantityByControllEvel();
		//国控、省控、市控废水间接排口数量
		List<PointQuantityByGroupGisVO> listIDWater = idPointListGisVODao.queryPointQuantityByControllEvel();
		//国控、省控、市控废气和废水排口数量
		List<PointQuantityByGroupGisVO> list= new ArrayList<>();
		//废气加废水总数
		long total = 0;
		for (int i = 0; i < listAir.size(); i++) {
			String controllName = listAir.get(i).getGroupName();
			Long count = listAir.get(i).getPointQuantity() + listDWater.get(i).getPointQuantity()
					 + listIDWater.get(i).getPointQuantity();
			total += count;
			list.add(new PointQuantityByGroupGisVO(controllName, count));
		}
		list.add(new PointQuantityByGroupGisVO("全部", total));
		return list;
		
	}
    
}
